package com.nutiteq.mapper.forms;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.nutiteq.MapComponent;
import com.nutiteq.mapper.MapScreen;
import com.nutiteq.mapper.Mapper;
import com.nutiteq.ui.EventDrivenPanning;
import com.nutiteq.ui.PanningStrategy;
import com.nutiteq.ui.ThreadDrivenPanning;

public class PanningList extends List implements CommandListener {
  public static final Command PANNING_STRATEGY = new Command("Panning", Command.ITEM, 0);

  private Command back;
  private Command select;

  private final MapComponent mapComponent;

  public PanningList(final MapComponent mapComponent) {
    super("Panning options", List.IMPLICIT);
    this.mapComponent = mapComponent;
    append("Threaded panning (default)", null);
    append("Event driven panning", null);

    back = new Command("Back", Command.BACK, 0);
    select = new Command("Select", Command.OK, 0);

    addCommand(back);
    setSelectCommand(select);
    addCommand(select);

    setCommandListener(this);
  }

  public void commandAction(final Command cmd, final Displayable d) {
    if (cmd == back) {
      Display.getDisplay(Mapper.instance).setCurrent(MapScreen.instance);
    } else if (cmd == select) {
      PanningStrategy panning = null;
      if (getSelectedIndex() == 0) {
        panning = new ThreadDrivenPanning();
      } else if (getSelectedIndex() == 1) {
        panning = new EventDrivenPanning();
      }
      mapComponent.setPanningStrategy(panning);
      Display.getDisplay(Mapper.instance).setCurrent(MapScreen.instance);
    }
  }
}
